package interfaces;

import java.util.Collections;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import javax.ejb.SessionContext;
import javax.ejb.Stateless;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import modelo.Users;

@Stateless(name = "GestionarUsuario", mappedName = "MiPrimerProyecto_GU")
public class GestionarUsuarioBean implements GestionarUsuario {
    @Resource
    SessionContext sessionContext;
    @PersistenceContext(unitName = "Model")
    private EntityManager em;

    public GestionarUsuarioBean() {
    }

    public Users persistUsers(Users users) {
        em.persist(users);
        return users;
    }

    public Users mergeUsers(Users users) {
        return em.merge(users);
    }

    public void removeUsers(Users users) {
        users = em.find(Users.class, users.getRfce());
        em.remove(users);
    }

    /** <code>select o from Users o</code> */
    public List<Users> getUsersFindAll() {
        return em.createNamedQuery("Users.findAll").getResultList();
    }

    /** <code>select o from Users o where o.fechacreacion between :paramFecha1 and :paramFecha2</code> */
    public List<Users> getUsersPorfecha(Date paramFecha1, Date paramFecha2) {
        return em.createNamedQuery("Users.porfecha").setParameter("paramFecha1",
                                                                  paramFecha1).setParameter("paramFecha2",
                                                                                            paramFecha2).getResultList();
    }

    /** <code>select o from Users o where o.fechacreacion= '2013-01-29' and o.sesion=1;</code> */
    public List<Users> getUsersFechaID() {
        return em.createNamedQuery("Users.fechaID").getResultList();
    }
    
    /** <code>select o from Users o where :paramAtributo like :paramValor</code> */
    public List<Users> getUsersBusqueda(String paramValor) {
        return em.createNamedQuery("Users.busqueda").setParameter("paramValor",paramValor).getResultList();
    }
    
    /** <code>select o from Users o where :paramAtributo like :paramValor</code> */
    public List<Users> getUsersBusquedaLog(String paramValor) {
        return em.createNamedQuery("Users.busquedaLog").setParameter("paramValor",paramValor).getResultList();
    }
    
    /** <code>select o from Users o where :paramAtributo like :paramValor</code> */
    public List<Users> getUsersBusquedaPass(String paramValor) {
        return em.createNamedQuery("Users.busquedaPass").setParameter("paramValor",paramValor).getResultList();
    }
    
    /** <code>select o from Users o where o.login = :paramLogin and o.password =:paramPass ;</code> */
    public List<Users> getUsersLogin(String paramLogin, String paramPass) {
        return em.createNamedQuery("Users.login").setParameter("paramLogin",
                                                                  paramLogin).setParameter("paramPass",
                                                                                            paramPass).getResultList();
    }


    @Override
    public List<Users> getUserOrdenados() {
        return em.createNamedQuery("Users.ordenados").getResultList(); 
    }
}
